/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.common.parsers.xml;

import com.blandware.atleap.common.parsers.SpecificPlainTextExtractor;
import com.blandware.atleap.common.parsers.exception.PlainTextExtractorException;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.InputStream;
import java.io.Writer;

/**
 * An extractor that extracts a plain text from XML documents.
 *
 * @see SpecificPlainTextExtractor
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 * &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.3 $ $Date: 2005/08/14 12:27:56 $
 */
public class XMLPlainTextExtractor implements SpecificPlainTextExtractor {
    public XMLPlainTextExtractor() {
    }

    /**
     * Extracts a plain text from an XML document.
     *
     * @param input the input stream that supplies an XML document for
     * extraction
     * @param output the writer that will accept the extracted text
     * @param encoding ignored
     * @throws PlainTextExtractorException throwed on exception raised during
     * extracting
     */
    public void extract(InputStream input, Writer output, String encoding)
            throws PlainTextExtractorException {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            parser.parse(input, new TextExtractingContentHandler(output));
        } catch (Exception e) {
            throw new PlainTextExtractorException(e);
        }
    }

    /**
     * @see com.blandware.atleap.common.parsers.SpecificPlainTextExtractor#getUsedEncoding()
     */
    public String getUsedEncoding() {
        return null;
    }
}
